package com.solocredit.finance.ui.user.act

import android.view.View
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import com.solocredit.finance.R
import com.solocredit.finance.app.MyApp
import com.solocredit.finance.base.utils.helper.ActivityStackManager
import com.solocredit.finance.databinding.ActMullaUserMessageBinding
import com.solocredit.finance.http.HttpHelper
import com.solocredit.finance.mvp.MvpAct
import com.solocredit.finance.ui.main.act.MullaMainAct
import com.solocredit.finance.ui.steps.act.MullaStepsAct
import com.solocredit.finance.ui.user.adapter.MullaUserMessageAdapter
import com.solocredit.finance.ui.user.impl.MullaUserMessagePresenterImpl
import com.solocredit.finance.ui.user.view.IMullaUserMessageView
import com.solocredit.finance.utils.event.MullaEventData
import com.solocredit.finance.utils.event.MullaEventType
import com.solocredit.finance.utils.helper.MullaCustomConfigsHelper
import com.solocredit.finance.utils.helper.SharedHelper
import com.solocredit.finance.utils.helper.TestDataLibInvoke
import org.json.JSONObject

/**
 * MullaFlow
 * @Author wwdeng
 * @CreateTime: 2024/8/19 17:05
 * @Annotation：消息列表
 */
class MullaUserMessageAct: MvpAct<IMullaUserMessageView, MullaUserMessagePresenterImpl>(),IMullaUserMessageView {

    lateinit var binding: ActMullaUserMessageBinding

    private var messageAdapter: MullaUserMessageAdapter? = null

    override fun createPresenter(): MullaUserMessagePresenterImpl {
        return MullaUserMessagePresenterImpl(this)
    }
    override fun getMullaRootView(): View {
        binding = ActMullaUserMessageBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initMullaView() {
        setMullaTitleBarVisibility(View.VISIBLE)
        setMullaBackBtnRes(R.mipmap.mulla_back_black)
        setMullaTextTitle(getString(R.string.message_title))
        setMullaRightBtnVisibility(View.GONE)

        initMullaRefreshLayout()
        initMullaRecyclerView()
    }

    override fun initMullaListener() {
    }

    override fun initMullaData() {
        showMullaLoading(isTOuchOut = false, isCancel = true)
        presenter.loanMullaMessageListAction()

        //牌照信息
        val licenseInfo = MullaCustomConfigsHelper.getMullaLicenseInfo()
        if (licenseInfo == null){
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.visibility = View.GONE
        } else {
            binding.licenseLayout.removeAllViews()
            binding.licenseLayout.addView(licenseView())
            binding.licenseLayout.visibility = View.VISIBLE
        }
    }

    /* 初始化刷新 */
    private fun initMullaRefreshLayout() {
        //禁止加载
        binding.refreshLayout.setEnableLoadMore(false)
        binding.refreshLayout.setEnableAutoLoadMore(false)
        binding.refreshLayout.setOnRefreshListener {
            presenter.loanMullaMessageListAction()
        }
        binding.refreshLayout.setOnLoadMoreListener {
            binding.refreshLayout.finishLoadMore()
        }
    }

    /**
     * 初始化消息列表
     */
    private fun initMullaRecyclerView() {
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = false
        binding.recyclerView.isFocusableInTouchMode = false //不需要焦点
        binding.recyclerView.layoutManager = LinearLayoutManager(this)
        messageAdapter = MullaUserMessageAdapter(binding.recyclerView)
        binding.recyclerView.adapter = messageAdapter
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        messageAdapter?.setOnItemChildClickListener { _, childView, position ->
            messageAdapter?.let {
                val item = it.getItem(position)
                when(item.optString("type")){
                    "REPAY_REMIDER",//还款提醒
                    "APPLY_STATUS_UPDATE" -> {//申请状态更新
                        //进入还款页
                        MyApp.app.publisher.publish(MullaEventData(MullaEventData.TYPE.MULLA_SHOW_REPAY))
                        ActivityStackManager.finishAllActivity(MullaMainAct::class.java)
                    }
                    "SIGN_REMINDER" -> {//签到
                        onIntent(MullaUserCheckInAct::class.java)
                    }
                    "COUPON_REMINDER" -> {//优惠券
                        onIntent(MullaUserCouponAct::class.java)
                    }
                    "UPGRADE_REMINDER" -> {//等级中心
                        onIntent(MullaUserVipAct::class.java)
                    }
                    else -> {//AD_MESSAGE  广告推广
                        val stepData = JSONObject(SharedHelper.getMullaSPString(SharedHelper.MULLA_SPKEY.MULLA_PROCESS))
                        if (stepData.optJSONArray("steps")?.length() == 0) {//流程已完成 - 进入首页
                            //进入还款页
                            MyApp.app.publisher.publish(MullaEventData(MullaEventData.TYPE.MULLA_SHOW_REPAY))
                            ActivityStackManager.finishAllActivity(MullaMainAct::class.java)
                        } else {
                            //流程未完成 - 进入流程页面
                            //上报
                            TestDataLibInvoke.takeFlexiMolaDeviceInfoTypeAction()
                            onIntent(MullaStepsAct::class.java)
                            finish()
                        }
                    }
                }
            }
        }
    }

    /**
     * 设置消息列表
     */
    override fun showMullsMessageList(msgList: ArrayList<JSONObject>) {
        messageAdapter?.data = msgList
    }

    override fun hideMullaLoading() {
        super.hideMullaLoading()
        binding.refreshLayout.finishRefresh()
    }

    override fun onStart() {
        super.onStart()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_MESSAGE_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        HttpHelper.addMullaEventAction(MullaEventType.PAGE.MULLA_PAGE_MESSAGE_END)
    }
}